package api

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"

	"schedulingsystem/backend/models"
)

// EmployeeHandler 处理员工相关的API请求
type EmployeeHandler struct {
	DB *gorm.DB
}

// NewEmployeeHandler 创建一个新的员工处理器
func NewEmployeeHandler(db *gorm.DB) *EmployeeHandler {
	return &EmployeeHandler{DB: db}
}

// GetEmployees 获取所有员工
func (h *EmployeeHandler) GetEmployees(c *gin.Context) {
	var employees []models.Employee
	result := h.DB.Find(&employees)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取员工列表失败"})
		return
	}

	c.JSON(http.StatusOK, employees)
}

// GetEmployee 获取单个员工
func (h *EmployeeHandler) GetEmployee(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的员工ID"})
		return
	}

	var employee models.Employee
	result := h.DB.First(&employee, id)
	if result.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "员工不存在"})
		return
	}

	c.JSON(http.StatusOK, employee)
}

// CreateEmployee 创建新员工
func (h *EmployeeHandler) CreateEmployee(c *gin.Context) {
	var employee models.Employee
	if err := c.ShouldBindJSON(&employee); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的员工数据"})
		return
	}

	result := h.DB.Create(&employee)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "创建员工失败"})
		return
	}

	c.JSON(http.StatusCreated, employee)
}

// UpdateEmployee 更新员工信息
func (h *EmployeeHandler) UpdateEmployee(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的员工ID"})
		return
	}

	var employee models.Employee
	result := h.DB.First(&employee, id)
	if result.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "员工不存在"})
		return
	}

	var updatedEmployee models.Employee
	if err := c.ShouldBindJSON(&updatedEmployee); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的员工数据"})
		return
	}

	// 更新字段
	employee.Name = updatedEmployee.Name
	employee.Department = updatedEmployee.Department
	employee.Position = updatedEmployee.Position
	employee.PhoneNumber = updatedEmployee.PhoneNumber
	employee.Email = updatedEmployee.Email
	employee.Preferences = updatedEmployee.Preferences

	result = h.DB.Save(&employee)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "更新员工失败"})
		return
	}

	c.JSON(http.StatusOK, employee)
}

// DeleteEmployee 删除员工
func (h *EmployeeHandler) DeleteEmployee(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的员工ID"})
		return
	}

	var employee models.Employee
	result := h.DB.First(&employee, id)
	if result.Error != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "员工不存在"})
		return
	}

	result = h.DB.Delete(&employee)
	if result.Error != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除员工失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "员工已删除"})
}